<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
	<#include '/common/head.html'>
	<script type="text/javascript">
		var $dg , $window, $obj={} ;
		$(function() {
			$dg = $('#dg').datagrid({
				title: '定时作业管理',
				url: '/scheduler/job/list', 
				method: 'GET', idField: 'jobId', nowrap: true, striped: true, singleSelect: false,
				checkbox: true, pagination: true, pageSize: 30, pageList: [30,40,50,100,200,500,1000], 
				autoRowHeight: true, fit: true, border: false, rownumbers: true, rownumberWidth: 40, toolbar: "#toolbars", 
				remoteSort: true, sortName: 'job_id', sortOrder: 'desc', queryParams: {},
				frozenColumns: [[
					{field: 'ck', checkbox:true}
				]],
				columns: [[
					{title: '作业ID', field: 'jobId', width: 80},
					{title: '作业名称', field: 'jobName', width: 200},
					{title: '作业分组名', field: 'jobGroup', width: 150},
					{title: 'Cron表达式', field: 'cronExpression', width: 200},
					{title: '状态', field: 'status', width: 60, align: 'center', formatter: function(value, row, index) {
						if(value==0) return '<i class="fa fa-circle" style="color:#15db81"></i>';
						else return'<i class="fa fa-circle" style="color:gray"></i>';
					}},
					{title: '执行类的全类名', field: 'className', width: 300},
					{title: 'Bean实例名', field: 'beanName', width: 300},
					{title: '执行的方法名', field: 'methodName', width: 110},
					{title: '参数', field: 'params', width: 400},
					{title: '备注信息', field: 'remarks', width: 400},
					{title: '创建时间', field: 'createTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
						return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
					}},
					{title: '创建者', field: 'createBy', width: 70},
					{title: '更新时间', field: 'updateTime', width: 130, align: 'center', sortable: true, formatter: function(value, row, index) {
						return value != null && value != '' ? $.date.format(value, 'yyyy-MM-dd HH:mm:ss') : '';
					}},
					{title: '更新者', field: 'updateBy', width: 70},
				]]
			});
		});
		
		$obj = {
			add: function() {
				$window = MM.addWindow({title: '添加', href: '/scheduler/job/form', width: 600, height: 460});
			},
			del: function() {
				var ids = MM.getSelectionsIds('#dg', 'jobId') ;
				if(ids) {
					$.messager.confirm('删除确认', '确定要删除？', function(r) {
						if(r) {
							$.get('/scheduler/job/delete/'+ids, function(result) {
								try {
									if(result.code == 200) {
										$obj.reload(); alertify.success(result.message);
									} else {
										alertify.warning(result.message);
									}
								} catch(e) {
									$.easyui.loaded(); console.error(e) ;
								}
							}, 'JSON').complete(function(){$.easyui.loaded();});
						}
					});
				} else {
					alertify.error('至少选择一条记录');
				}
			},
			
			createScheduleJob: function() {
				$window = MM.addWindow({title: '添加', href: '/scheduler/job/form', width: 600, height: 460});
			},
			rescheduleJob: function() {
				var row = $dg.datagrid('getSelected') ;
				if(null != row && row.jobId != "") {
					$window = MM.addWindow({title: '修改', href: '/scheduler/job/form?jobId='+row.jobId, width: 600, height: 320});
				} else {
					alertify.error('请选择一条记录');
				}
			},
			pauseJob: function() {
				operationAjax("/scheduler/job/pauseJob", '暂停作业') ;
			},
			resumeJob: function() {
				operationAjax("/scheduler/job/resumeJob", '恢复作业') ;
			},
			deleteJob: function() {
				operationAjax("/scheduler/job/deleteJob", '删除作业') ;
			},
			runNowJob: function() {
				operationAjax("/scheduler/job/runNowJob", '立即执行') ;
			},
			reload: function() {
				$dg.datagrid('reload'); $dg.datagrid('clearSelections');$dg.datagrid('clearChecked');
			}
		}
		
		function operationAjax(url, info) {
			var rows = $dg.datagrid('getSelections') ;
			var jobIds = [] ;
			var jobGroups = [] ;
			
			$.messager.confirm(info+'确认', '确定要'+info+'？', function(r) {
				if(r) {
					if(rows.length > 0) {
						$.each(rows, function(index, item){
							jobIds.push(item.jobId) ;
							jobGroups.push(item.jobGroup) ;
						});
					
						$.ajax({
						    url: url,
						    type:"POST",
						    dataType:"JSON",
						    contentType:"application/json",
						    data: JSON.stringify({"jobIds": jobIds, "jobGroups": jobGroups}),
						    success:function(result){
						    	try {
									if(result.code == 200) {
										$obj.reload(); alertify.success(result.message);
									} else {
										alertify.warning(result.message);
									}
								} catch(e) {
									$.easyui.loaded(); console.error(e) ;
								}
						    },
						    error:function(data){
						    	$.easyui.loaded();
						    }
						});
					} else {
						alertify.error('请选择一条记录');
					}
				}
			});
		}
	</script>
	
</head>

<body>
	<div id="dg">
		<div id="toolbars"> 
			<a onclick="$obj.createScheduleJob();" class="easyui-linkbutton" data-options="plain:true, iconCls:'icon-hamburg-project'">创建作业</a> 
			<a onclick="$obj.rescheduleJob();" class="easyui-linkbutton" data-options="plain:true, iconCls:'icon-hamburg-date'">编辑作业</a> 
			<a onclick="$obj.pauseJob();" class="easyui-linkbutton" data-options="plain:true, iconCls:'icon-hamburg-feed'">暂停作业</a> 
			<a onclick="$obj.resumeJob();" class="easyui-linkbutton" data-options="plain:true, iconCls:'icon-hamburg-bug'">恢复作业</a> 
			<a onclick="$obj.deleteJob();" class="easyui-linkbutton" data-options="plain:true, iconCls:'icon-hamburg-drawings'">删除作业</a> 
			<a onclick="$obj.runNowJob();" class="easyui-linkbutton" data-options="plain:true, iconCls:'icon-hamburg-statistics'">立即运行</a> 
			<a onclick="$obj.reload();" class="easyui-linkbutton" data-options="plain:true, iconCls:'icon-reload'">刷新</a> 
		</div>
	</div>
</body>
</html>